package core.database;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

@Entity(name = "core.Node")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name = "nodes")
public class Node implements Serializable {
	private static final long	serialVersionUID	= -476332897260986537L;

	@Id
	@Column(name = "id")
	@GeneratedValue(strategy = GenerationType.AUTO)
	private Long				id					= null;

	@Column(name = "name")
	// TODO: , unique = true, nullable = false
	private String				name;

	@Column(name = "address")
	// TODO: , nullable = false
	private String				address;

	@Column(name = "ip", nullable = false, length = 40)
	private String				ip;

	@Column(name = "port", nullable = false, length = 5)
	private String				port;

	@Column(name = "level", nullable = false)
	private int					level;

	@Column(name = "nodeType", nullable = false)
	@Enumerated(EnumType.ORDINAL)
	private NodeType			nodeType;

	public Node() {
	}

	public Long getId() {
		return id;
	}

	@SuppressWarnings("unused")
	private void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getIp() {
		return ip;
	}

	public void setIp(String ip) {
		this.ip = ip;
	}

	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}

	public int getLevel() {
		return level;
	}

	public void setLevel(int level) {
		this.level = level;
	}

	public NodeType getNodeType() {
		return nodeType;
	}

	public void setNodeType(NodeType nodeType) {
		this.nodeType = nodeType;
	}

	public String getLocatorURI() {
		return Node.getLocatorURI(getIp(), getPort());
	}

	public static String getLocatorURI(String ip, String port) {
		return ip + ":" + port;
	}

	public static String getIp(String locatorUri) {
		return locatorUri.split(":")[0];
	}

	public static String getPort(String locatorUri) {
		return locatorUri.split(":")[1];
	}

	public String toString() {
		return "(" + getId() + ")" + getLocatorURI();
	}
}
